package com.kaifamiao.map;

import java.util.*;

/**
 * Map接口中的抽象方法:
 *      Set<K> keySet()
 *      Collection<V> values()
 *      Set<Map.Entry<K, V>> entrySet()
 **/
public class MapInstanceMethods2 {

    public static void main(String[] args) {
        Map<String,Integer> map = new HashMap<>();
        map.put( "王语嫣" , 18 );
        map.put( "阿紫", 19 );
        map.put( "阿朱", 20 );
        map.put( "钟灵", 17 );
        System.out.println( map );

        // Set 是 Collection 的子接口
        Set<String> keys = map.keySet();
        Iterator<String> itor = keys.iterator();
        while( itor.hasNext() ) {
            String key = itor.next();
            Integer value = map.get( key );
            System.out.println( key + " : " + value );
        }

        System.out.println( "- - - - - -" );

        Collection<Integer> values = map.values();
        for ( Integer value : values ) {
            System.out.println( value );
        }

        System.out.println( "- - - - - -" );

        Set< Map.Entry<String,Integer> > entries = map.entrySet();
        // Set 继承了 Collection 接口，间接继承了 Iterable 接口
        for ( Map.Entry<String,Integer> entry : entries ) {
            System.out.println( entry.getKey() + ", " + entry.getValue() );
        }

    }

}
